package cn.shuangze.assetsms.view;

import cn.shuangze.assetsms.component.MyJTable;
import cn.shuangze.assetsms.dao.AssetsTrjnDao;
import cn.shuangze.assetsms.dao.PersonInfoDao;
import cn.shuangze.assetsms.dao.impl.AssetsTrjnDaoJDBCImpl;
import cn.shuangze.assetsms.dao.impl.PersonInfoDaoJDBCImpl;
import cn.shuangze.assetsms.dao.test.OptionType;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;

public class ShowAssetsTrjn extends JDialog {

    //定义DAO
    private AssetsTrjnDao assetsTrjnDao;
    private PersonInfoDao personInfoDao;
    private OptionType flag;
    private Thread showThread;

    //框架的大小
    private Dimension viewSize = new Dimension(600, 300);

    //定义图形界面元素
    private JTable jTable;
    private DefaultTableModel tableModel;


    public ShowAssetsTrjn(OptionType flag) {

        this.flag = flag;

        assetsTrjnDao = new AssetsTrjnDaoJDBCImpl();
        personInfoDao = new PersonInfoDaoJDBCImpl();

        showThread= new Thread(() -> {
            try {
                showTableData();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, "窗口初始化错误，错误原因：" + e.getMessage(),
                        "错误", JOptionPane.ERROR_MESSAGE);
            }
        });

        //界面初始化
        init();
    }

    private void init() {

        this.setModal(true);
        this.setSize(viewSize);
        this.setResizable(true);

        //设置标题
        if (flag == OptionType.USING) {
            this.setTitle("资产领用查询");
        } else if (flag == OptionType.BACK) {
            this.setTitle("资产归还查询");
        } else if (flag == OptionType.INVALID) {
            this.setTitle("资产报废查询");
        }

        //设置运行时窗口的位置
        this.setLocationRelativeTo(null);

        //中部面板的布局
        tableModel = new DefaultTableModel();  // 创建默认表格模式
        jTable = new MyJTable(tableModel);  // 创建表格 禁用编辑功能
        ListSelectionModel listSelectionModel = jTable.getSelectionModel();
        listSelectionModel.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  // 设置表格选择模式为单一选择
        JScrollPane jScrollPane1 = new JScrollPane(jTable);  // 创建一个滚动面板，包含表格
        jScrollPane1.setPreferredSize(viewSize);  //设置滚动面板大小
        DefaultTableCellRenderer cr = new DefaultTableCellRenderer();
        cr.setHorizontalAlignment(JLabel.CENTER);  //表格内容居中
        jTable.setDefaultRenderer(Object.class, cr);
        //jTable.getTableHeader().setReorderingAllowed(false);  //禁止表头列拖动
        showThread.start();
        this.add(jScrollPane1);

    }


    private void showTableData() throws Exception {

        String type = null;

        if (flag == OptionType.USING) type = "领用";
        else if (flag == OptionType.BACK) type = "归还";
        else if (flag == OptionType.INVALID) type = "报废";

        String[][] colValue = assetsTrjnDao.findType(type);
        String[][] personValue = personInfoDao.findAll();

        //由编号匹配操作人姓名
        for (int i = 0; colValue != null && i < colValue.length; i++) {
            for (String[] personStr : personValue) {
                if (colValue[i][4].equals(personStr[0])) {
                    colValue[i][4] = personStr[1];
                }
            }
        }

        String[] colName = {"编号", "操作类型", "资产编号", "操作时间", "操作人员", "用途说明", "备注信息"};
        tableModel.setDataVector(colValue, colName);  //添加数据到表格
        jTable.revalidate();  //刷新表格
    }

}
